﻿using System;
using System.Text;
using System.Data;
using System.Data.OleDb;
using System.Collections.Generic;
using System.Reflection;
namespace Data
{
	 	//CMember
		public class CMember
	{
            private DataAccess Access = new Data.DataAccess();
		/// <summary>
        /// 主键
        /// </summary>
        private readonly string primaryKey= "Id";
		/// <summary>
        /// 添加
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public long Add(Model.CMember model)
        {
            return Access.Add<Model.CMember>(model, primaryKey);
        }
        /// <summary>
        /// 修改
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public int Update(Model.CMember model)
        {
            return Access.Update<Model.CMember>(model, primaryKey);
        }
        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="Id"></param>
        /// <returns></returns>
        public bool Delete(long Id)
        {
            return Access.Delete<Model.CMember>(Id, primaryKey);
        }
        /// <summary>
        /// 单条
        /// </summary>
        /// <param name="Id"></param>
        /// <returns></returns>
        public Model.CMember GetModel(long Id)
        {
            return Access.Get<Model.CMember>(Id, primaryKey);
        }
        /// <summary>
        /// 总数据数
        /// </summary>
        /// <returns></returns>
        public int GetAll_Count()
        {
            return Access.GetCount<Model.CMember>(primaryKey);
        }
        public int GetAll_Count_Canuse()
        {
            return Access.GetCount<Model.CMember>(primaryKey, " uesrState<>0");
        }
        /// <summary>
        /// 抽取分页数据
        /// </summary>
        public List<Model.CMember> GetPageList_Where(int PageIndex, int PageSize, string u_password,out int totalCount)
        {          
            string strWhere = GetSqlWhere(u_password);
            totalCount = Access.GetCount<Model.CMember>(primaryKey, strWhere);
            return Access.GetPage_List<Model.CMember>(PageIndex, PageSize, primaryKey, primaryKey+" desc", strWhere);
        }
        public List<Model.CMember> Getall(int PageIndex, int PageSize,  out int totalCount)
        {
            string strWhere = "  uesrState<>0";
            totalCount = Access.GetCount<Model.CMember>(primaryKey, strWhere);
            return Access.GetPage_List<Model.CMember>(PageIndex, PageSize, primaryKey, primaryKey+" asc", strWhere);
        }
        /// <summary>
        /// 拼接条件
        /// </summary>
        public string GetSqlWhere(string u_password)
        {
            StringBuilder sqlWhere = new StringBuilder();

            sqlWhere.Append(" uesrState<>0");

            if (!string.IsNullOrEmpty(u_password) && !string.IsNullOrEmpty(u_password.Trim()))
            {
                sqlWhere.Append(" and u_password like '%" + u_password + "%'");
            }         
            return sqlWhere.ToString();
        }
        /// <summary>
        /// 批删
        /// </summary>
        /// <param name="Ids"></param>
        /// <returns></returns>
        public int Drop(string Ids)
        {
            return Access.Drop<Model.CMember>(Ids, primaryKey);
        }
        public bool GetIsExitmob(string mob)
        {
            if (!string.IsNullOrEmpty(mob) && !string.IsNullOrEmpty(mob.Trim()))
            {
                StringBuilder sqlWhere = new StringBuilder();
                sqlWhere.Append(" mob='" + mob.Trim() + "'");
                return (Access.GetCount<Model.CMember>("Id", sqlWhere.ToString()) > 0);
            }
            else
            {
                return true;
            }
        }
        public bool GetIsExitNickname(string nickname)
        {
            if (!string.IsNullOrEmpty(nickname) && !string.IsNullOrEmpty(nickname.Trim()))
            {
                StringBuilder sqlWhere = new StringBuilder();
                sqlWhere.Append(" nickname='" + nickname.Trim() + "'");
                return (Access.GetCount<Model.CMember>("Id", sqlWhere.ToString()) > 0);
            }
            else
            {
                return true;
            }
        }
        public Model.CMember userLogin(string strusername, string password)
        {
            if (!string.IsNullOrEmpty(strusername) && !string.IsNullOrEmpty(strusername.Trim()) && !string.IsNullOrEmpty(password) && !string.IsNullOrEmpty(password.Trim()))
            {
                System.Data.Common.DbParameter[] p = new System.Data.Common.DbParameter[0];
                StringBuilder sqlWhere = new StringBuilder();
                sqlWhere.Append(" (nickname='" + strusername.Trim()
                    + "' or mob='" + strusername.Trim()
                    + "' or email='" + strusername.Trim()
                    + "') and u_password='" + password.Trim() + "' AND uesrState<>0");
                return Access.Get<Model.CMember>(sqlWhere.ToString(), p);
            }
            else
            {
                return null;
            }
        }
        public Model.CMember GetOneBymob(string strmob)
        {
            if (!string.IsNullOrEmpty(strmob) && !string.IsNullOrEmpty(strmob.Trim()))
            {
                System.Data.Common.DbParameter[] p = new System.Data.Common.DbParameter[0];
                StringBuilder sqlWhere = new StringBuilder();
                sqlWhere.Append(" mob='" + strmob.Trim() + "' AND uesrState<>0");
                return Access.Get<Model.CMember>(sqlWhere.ToString(), p);
            }
            else
            {
                return null;
            }
        }
        public Model.CMember GetOneBynickname(string nickname)
        {
            if (!string.IsNullOrEmpty(nickname) && !string.IsNullOrEmpty(nickname.Trim()))
            {
                System.Data.Common.DbParameter[] p = new System.Data.Common.DbParameter[0];
                StringBuilder sqlWhere = new StringBuilder();
                sqlWhere.Append(" nickname='" + nickname.Trim() + "' AND uesrState<>0");
                return Access.Get<Model.CMember>(sqlWhere.ToString(), p);
            }
            else
            {
                return null;
            }
        }
        public string GetTheEmail(int Id)
        {
            if (Id > 0)
            {
                StringBuilder sbrSQL = new StringBuilder();
                sbrSQL.Append("SELECT TOP 1 email FROM [CMember] WHERE uesrState<>0 AND Id=");
                sbrSQL.Append(Id);
                DataTable dt = new DataAccess().GetDataTable<Model.CMember>(sbrSQL.ToString());
                if (dt != null && dt.Rows.Count > 0)
                {
                    return dt.Rows[0][0].ToString();
                }
            }
            return null;
        }
        public List<Model.CMember> GetAllCmember_Canuse()
        {
            StringBuilder sbrSQL = new StringBuilder();
            sbrSQL.Append("SELECT * FROM [CMember]");
            DataTable dt = new DataAccess().GetDataTable<Model.CMember>(sbrSQL.ToString());
            if (dt != null && dt.Rows.Count > 0)
            {
                return ConvertHelper<Model.CMember>.ConvertToModelList(dt);
            }
            return null;
        }
        public List<Model.CMember> GetAllCmember_Canuse(long mob)
        {
            StringBuilder sbrSQL = new StringBuilder();
            sbrSQL.Append("SELECT * FROM [CMember] WHERE uesrState<>0");
            sbrSQL.Append(" and mob like '%" + mob + "%'");
            DataTable dt = new DataAccess().GetDataTable<Model.CMember>(sbrSQL.ToString());
            if (dt != null && dt.Rows.Count > 0)
            {
                return ConvertHelper<Model.CMember>.ConvertToModelList(dt);
            }
            return null;
        }
	}
}